package top.kaoshanji.p2ch10c;

import java.util.ArrayList;
import java.util.List;

/**
 * 代码清单-p2ch10c-184页a：定义一个验证器
 * @author kaoshanji
 * ^_^
 * create time 2025/4/8 18:34
 */
public class ProductValidator {

    public List<String> validate(ProductForm productForm) {
        List<String> errors = new ArrayList<>();

        String name = productForm.getName();
        if (name == null || name.trim().isEmpty()) {
            errors.add("产品名称不能为空");
        }

        String price = productForm.getPrice();
        if (price == null || price.trim().isEmpty()) {
            errors.add("Product must have a price");
        } else {
            try {
                Float.parseFloat(price);
            } catch (NumberFormatException e) {
                errors.add("Invalid price value");
            }
        }

        return errors;
    }

}
